<?php

namespace app\admin\controller\system;

use app\common\annotation\NodeAnnotation;
use app\common\controller\Admin;
use app\common\traits\ControllerCurd;
use think\Db;
use think\Request;

/**
 * Class Administrator
 * @package app\admin\controller\system
 * @NodeAnnotation(
 *     title="管理员",
 *     parent="admin/system.setting",
 *     is_menu=0,
 *     is_view=0
 * )
 */
class Administrator extends Admin
{
    use ControllerCurd;

    public $model;

    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\system\Administrator();
    }

    /**
     * @NodeAnnotation(
     *     title="管理员设置",
     *     parent="admin/system.administrator",
     *     MenuRoot="admin/system.setting"
     * )
     */
    public function index()
    {
        $keywords = $this->request->param("keywords");

        $where[] = ["is_admin","eq",1];

        if($keywords) array_push($where,["fullname|username|phone|email","eq",$keywords]);

        $this->assign([
            "adminUserList" => $this->model->selectPageDatas(1,20,$where),
            "search"        => $keywords
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="添加管理员",
     *     parent="admin/system.Role",
     *     MenuRoot="admin/system.setting",
     *     is_menu=0
     * )
     */
    public function addadmin(Request $request)
    {
        if($request->isPost())
        {
            $input = $request->post();
            if(isset($input['password'])) $input['password'] = Passwordmd5($input['password']);
            $this->add(url("admin/system.administrator/index"),function(){},$input);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="修改管理员",
     *     parent="admin/system.administrator",
     *     MenuRoot="admin/system.setting",
     *     is_menu=1
     * )
     */
    public function editadmin(Request $request,$id=0)
    {
        if($request->isPost())
        {
            $input = $request->post();
            if(isset($input['password'])) $input['password'] = Passwordmd5($input['password']);
            $this->edit(url("admin/system.administrator/index"),function(){},$input);
        }
        $user = $this->model->findData($id);
        $this->assign([
            "adminUser" => $user?$user:get_user()
        ]);
    }

    /**
     * @NodeAnnotation(
     *     title="删除管理员",
     *     parent="admin/system.administrator",
     *     MenuRoot="admin/system.setting",
     *     is_menu=0,
     *     is_view=0
     * )
     */
    public function deladmin(Request $request)
    {
       $this->del();
    }

    /**
     * @NodeAnnotation(
     *     title="重置管理员密码",
     *     parent="admin/system.administrator",
     *     MenuRoot="admin/system.setting",
     *     is_menu=0,
     *     is_view=0
     * )
     */
    public function resetadminpwd(Request $request)
    {
        $this->edit(url("admin/system.administrator/index"));
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="配置角色",
     *     parent="admin/system.administrator",
     *     MenuRoot="admin/system.setting",
     *     is_menu=0,
     *     is_view=0
     * )
     */
    public function setaccess(Request $request)
    {
        //获取角色列表
        $uid = $request->param("uid",0,"intval");
        $accessList = Db::name("auth_group_access")
            ->where("uid",$uid)
            ->column("group_id");

        if($request->isAjax() && $request->isPost())
        {
            $input = $request->post();
            if(isset($input['role_id'])) $input['role_id'] = array_unique($input['role_id']);
            if(!empty($input['role_id']) && !empty($input['uid']) && is_array($input['role_id']))
            {
                foreach ($input['role_id'] as $item) {
                    $new_input[] = [
                        'uid'      => $input['uid'],
                        'group_id' => $item
                    ];

                }
            }
            if(isset($new_input))
            {
                if($accessList){
                    Db::name("auth_group_access")->where("uid",$uid)->delete();
                }
               $res =  Db::name("auth_group_access")->insertAll($new_input);
               if($res){
                   $this->success("配置成功");
               }else{
                   $this->error("配置失败");
               }
            }
        }

        //角色清单
        $roleList = (new \app\admin\model\system\Role())->selectRole();
        foreach ($roleList as $key => $item)
        {
            $roleList[$key]['pid']=0;
            if(in_array($item['id'],$accessList))
            {
                $roleList[$key]['checked']=true;
            }else{
                $roleList[$key]['checked']=false;
            }
        }
        return json($roleList);
    }



}